package com.ftdigital.primemovers_operators.fragment;


import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.ftdigital.primemovers_operators.MainActivity;
import com.ftdigital.primemovers_operators.MyApplication;
import com.ftdigital.primemovers_operators.R;
import com.ftdigital.primemovers_operators.adpter.DayInvertoryListViewAdapter;
import com.ftdigital.primemovers_operators.adpter.TeamAddOilHistoryListViewAdapter;
import com.ftdigital.primemovers_operators.config.GlobalData;
import com.ftdigital.primemovers_operators.config.RequestAddr;
import com.ftdigital.primemovers_operators.model.BaseModel;
import com.ftdigital.primemovers_operators.model.DayInventory;
import com.ftdigital.primemovers_operators.model.Entity;
import com.ftdigital.primemovers_operators.model.HistoryData;
import com.ftdigital.primemovers_operators.model.OilHistroryEntity;
import com.ftdigital.primemovers_operators.model.OilReprotEntity;
import com.ftdigital.primemovers_operators.util.http.AjaxCallBack;
import com.google.gson.JsonObject;
import com.google.gson.JsonSyntaxException;

import org.androidannotations.annotations.AfterExtras;
import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.FragmentArg;
import org.androidannotations.annotations.ViewById;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.Serializable;

/**
 * A simple {@link Fragment} subclass.
 */
@EFragment(R.layout.fragment_team_add_oil_history)
public class TeamAddoilHistoryFragment extends CoreFragment<OilHistroryEntity> {
    @ViewById(R.id.back_btn)
    ImageButton backBtn;

    @ViewById(R.id.toptitle_title)
    TextView topTitle;

    @ViewById(R.id.listView)
    RecyclerView listView;

    @FragmentArg("car_id")
    int car_id;

    TeamAddOilHistoryListViewAdapter teamAddOilHistoryListViewAdapter;
    @Override
    public String getCachePrifix() {
        return "TeamAddoilHistoryFragment";
    }

    @AfterViews
    @Override
    public void initView() {
        backBtn.setVisibility(View.VISIBLE);
        topTitle.setText(getResources().getString(R.string.title_add_oil_history));
        listView.setLayoutManager(new LinearLayoutManager(getActivity()));

        loadData();
    }

    @Override
    public void requestData() {
        requestParams = "car_id="+ car_id
        +"&supplier_id="+MyApplication.userInfo.getData().getSupplier_id();
        requestUrl = RequestAddr.teamHistory + requestParams;
        xtHttp.get(requestUrl, new AjaxCallBack<Object>() {
            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                super.onFailure(t, errorNo, strMsg);
            }

            @Override
            public void onSuccess(Object object) {
                super.onSuccess(object);
                //设置最新更新时间
                MyApplication.pushRefreashTime(getCache(), System.currentTimeMillis());
                executeParserTask(object);
            }
        });
    }


    @Override
    protected Object readList(Serializable serializable) {
        return serializable;
    }

    @Click(R.id.back_btn)
    @Override
    public void Click(View view) {
        switch (view.getId()) {
            case R.id.back_btn:
                MainActivity.getInstance().getSupportFragmentManager().popBackStack();
                break;
        }
    }

    @Override
    protected void executeOnLoadData(OilHistroryEntity oilHistroryEntity) {
        teamAddOilHistoryListViewAdapter = new TeamAddOilHistoryListViewAdapter(getActivity().getApplicationContext(), oilHistroryEntity);
        listView.setAdapter(teamAddOilHistoryListViewAdapter);
    }


    OilHistroryEntity oilHistroryEntity;
    @Override
    protected OilHistroryEntity parseList(Object object) {
        JSONObject jsonObject = null;
        try {
            jsonObject = new JSONObject(object.toString());
            if (jsonObject.getBoolean("flag")) {
                oilHistroryEntity = new OilHistroryEntity();
                JSONArray jsonArray = jsonObject.getJSONArray("data");
                HistoryData historyData;
                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONObject dataObject = new JSONObject();
                    dataObject = jsonArray.getJSONObject(i);
                    historyData = new HistoryData();
                    historyData.setCar_num(dataObject.getString("car_num"));
                    historyData.setConvention_price(dataObject.getInt("convention_price"));
                    historyData.setCompany_name(dataObject.getString("company_name"));
                    historyData.setQty(dataObject.getInt("qty"));
                    historyData.setDate(dataObject.getString("date"));
                    historyData.setGasstation_name(dataObject.getString("gasstation_name"));
                    historyData.setTotal_price(dataObject.getInt("total_price"));
                    oilHistroryEntity.getData().add(historyData);
                }
            }else {
                return null;
            }
        } catch (JSONException e) {
            e.printStackTrace();
            return null;
        }

        return oilHistroryEntity;
    }




}
